package tgc.rj.zz.controller;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//import net.minidev.json.JSONArray;
import tgc.rj.zz.custom.AjaxResult;
import tgc.rj.zz.custom.TablePageable;
import tgc.rj.zz.entity.User;
import tgc.rj.zz.form.UserForm;
import tgc.rj.zz.security.SysUser;
import tgc.rj.zz.service.SysUserService;
import tgc.rj.zz.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;
	@Autowired
	private SysUserService sysuserSercice;

	@RequestMapping("/manage")
	public String user(){
		return "user/manage";
	}
	
	@RequestMapping("/page")
	@ResponseBody
	public Object page(UserForm form,TablePageable pageParam) {
		PageRequest pageable = pageParam.bulidPageRequest();
		Specification<User> spec = buildSpec(form);
		Page<User> page=userService.findAll(spec, pageable);
		HashMap<String, Object> result = AjaxResult.bulidPageResult(page);
		return result;
	}
	
	public Specification<User> buildSpec(UserForm form){
		Specification<User> spec = new Specification<User>() {

			@Override
			public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules = new HashSet<>();
				System.out.println("===="+form.getSearch());
				if(StringUtils.hasText(form.getName())){
					rules.add(cb.like(root.get("username"), "%"+form.getUsername()+"%"));
				}
				if(StringUtils.hasText(form.getSearch())){
					Predicate username = cb.like(root.get("username"), "%"+form.getSearch()+"%");
					//Predicate type = cb.like(root.get("type"), "%"+form.getSearch()+"%");
					//Predicate tbClass = cb.like(root.get("tbClass"), "%"+form.getSearch()+"%");
					//Predicate curriculum = cb.like(root.get("curriculum"), "%"+form.getSearch()+"%");
					//Predicate opplyTime = cb.like(root.get("opplyTime"), "%"+form.getSearch()+"%");
					Predicate or = cb.or(username);
					rules.add(or);
				}
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
			
		};
		return spec;
	}
	
	@RequestMapping("/edit")
	public void edit(Integer id,ModelMap map){
		User user=new User();
		if(id!=null){
			user = userService.findById(id);
		}
		map.put("user",user);
	}
	
	@RequestMapping("/save")
	@ResponseBody
	public Object save(UserForm form){
		try {
			User model = new User();
			BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
			model.setPassword(encoder.encode("123456"));
			model.setIdentity(2);
			BeanUtils.copyProperties(form, model);
			userService.save(model);
			return new AjaxResult(true, "保存成功！");
		} catch (Exception e) {
			return new AjaxResult(false, "保存失败！");
		}
	}
	@RequestMapping("/delete")
	@ResponseBody
	public void del(Integer id){
		userService.deleteById(id);
	}
	@RequestMapping("/search")
	@ResponseBody
	public List<SysUser> search(String sname1,ModelMap map){
		System.out.println("==="+sname1);
		List<SysUser> list = sysuserSercice.findByUsernameLike("%"+sname1+"%");
		//map.put("list", list);
		/*String json = JSONArray.toJSONString(list);
		System.out.println(json+"-------");*/
		//HashMap<String, Object> result = AjaxResult.bulidPageResult(list);
		return  list;
		
		
	}
	
}
